/**
 *Copyright (c) 2024 watereyes
 * safetymanagement is licensed under Mulan PubL v2.
 * You can use this software according to the terms and conditions of the Mulan
 * PubL v2.
 * You may obtain a copy of Mulan PubL v2 at:
 *          http://license.coscl.org.cn/MulanPubL-2.0
 * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
 * EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
 * MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
 * See the Mulan PubL v2 for more details.
 */
package com.koron.report.mapper.statistics;

import com.koron.report.bean.StatisticsBean;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.util.Date;
import java.util.List;

/**
 * 下达任务完成率相关指标统计
 */
public interface UserTaskRateStatisticsMapper {

    /**
     * 查询任务实际完成多少或者计划完成多少,根据参数isFinish来区分,统计层级为人员
     * @param isFinish 不传，统计指定时间计划完成的，传2：统计指定时间实际完成的
     * @param startTime 开始时间
     * @param endTime 结束时间
     * @return
     */
    @Select("<script>" +
            "select receiver_code as code, count(*) as value" +
            " from task_user_relation" +
            " where (update_time between #{startTime} and #{endTime})" +
            "<if test='isFinish!=null'> and is_finish=#{isFinish} </if>" +
            " GROUP BY receiver_code"+
            "</script>")
    List<StatisticsBean> countUserTaskFinishOrTotalByPerson(@Param("isFinish") Integer isFinish, @Param("startTime") Date startTime,
                                                            @Param("endTime")  Date endTime);

    /**
     * 查询任务实际完成多少或者计划完成多少,根据参数isFinish来区分,统计层级为部门
     * @param isFinish 不传，统计指定时间计划完成的，传2：统计指定时间实际完成的
     * @param startTime 开始时间
     * @param endTime 结束时间
     * @return
     */
    @Select("<script>" +
            "select receiver_department_code as code, count(*) as value" +
            " from task_user_relation" +
            " where (update_time between #{startTime} and #{endTime})" +
            "<if test='isFinish!=null'> and is_finish=#{isFinish} </if>" +
            " GROUP BY receiver_department_code"+
            "</script>")
    List<StatisticsBean> countUserTaskFinishOrTotalByDept(@Param("isFinish") Integer isFinish, @Param("startTime") Date startTime,
                                                          @Param("endTime")  Date endTime);

    /**
     * 查询任务实际完成多少或者计划完成多少,根据参数isFinish来区分,统计层级为水司,片区，集团
     * 通过codeList来区分，codeList仅包含某个水司下的部门编码即查询水司,仅包含某个片区下所有水司的部门编码即查询水司
     *                     不传codeList查询集团
     * @param isFinish 不传，统计指定时间计划完成的，传2：统计指定时间实际完成的
     * @param startTime 开始时间
     * @param endTime 结束时间
     * @return
     */
    @Select("<script>" +
            "select count(*)" +
            " from task_user_relation" +
            " where (update_time between #{startTime} and #{endTime})" +
            "<if test='isFinish!=null'> and is_finish=#{isFinish} </if>" +
            "<if test='null!=codeList'> " +
            " and receiver_department_code in " +
            "<foreach collection='codeList' item='code' open='(' close=')' separator=','> #{code}</foreach>" +
            "</if>" +
            "</script>")
    Integer countUserTaskFinishOrTotalByCorpOrDitrictOrGroup(@Param("isFinish") Integer isFinish, @Param("startTime") Date startTime,
                                             @Param("endTime")  Date endTime,@Param("codeList") List<String>codeList);
}
